// 实验4 互斥信号量(具有优先级继承机制的信号量)

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"

int temp = 0;

// 1. 创建互斥信号量
SemaphoreHandle_t my_mutex;

// 2. 获取和释放1
void taskA(void* param)
{
    while (1)
    {
        // 先获取
        xSemaphoreTake(my_mutex, portMAX_DELAY);        
        ESP_LOGI("taskA","value = %d", temp++);
        vTaskDelay(pdMS_TO_TICKS(500));
        // 用完后释放
        xSemaphoreGive(my_mutex);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

// 3. 获取和释放2
void taskB(void* param)
{
    while (1)
    {
        // 先获取
        xSemaphoreTake(my_mutex, portMAX_DELAY);        
        ESP_LOGI("taskB","value = %d", temp++);
        vTaskDelay(pdMS_TO_TICKS(500));
        // 用完后释放
        xSemaphoreGive(my_mutex);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}


void app_main(void)
{
    // 0. 初始化互斥信号量
    my_mutex = xSemaphoreCreateMutex();

    xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 1);
    xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, NULL, 1);


}

 